package com.dts.evaluate.service;

import android.app.Application;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.dts.evaluate.R;
import com.dts.evaluate.callback.MyPictureCallback;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.controller.impl.DataImpl;
import com.dts.evaluate.controller.intf.DataInterface;
import com.dts.evaluate.ui.view.CameraView;
import com.dts.evaluate.utils.LogUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class CameraPictureService extends Service {

    private WindowManager.LayoutParams layoutParams;
    private WindowManager windowManager;
    private View rootView;
    private CameraView cameraView;

    public CameraPictureService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        final String projectId = intent.getStringExtra(Constants.PROJECT_ID);

        LogUtil.v("开始拍照onStartCommand  projectId:"+projectId);
        long time = System.currentTimeMillis();
        final String name = String.valueOf(time) + ".jpg";
        cameraView.startTakePicture(String.valueOf(time), new MyPictureCallback() {
            @Override
            public void callback(String path) {
                LogUtil.v("照相成功："+path);
                windowManager.removeView(rootView);
                commitPicPath(name,path,projectId);
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }




    /**
     * 初始化View
     */
    private void init(){
        layoutParams = new WindowManager.LayoutParams();
        windowManager = (WindowManager) getApplication().getSystemService(Application.WINDOW_SERVICE);

        //设置window type
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //设置图片格式，效果背景透明
        layoutParams.format = PixelFormat.RGB_888;
        //设置悬浮窗口不能聚焦
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        //设置悬浮窗口在左上
        layoutParams.gravity = Gravity.LEFT|Gravity.TOP;

        layoutParams.x = 0;
        layoutParams.y = 0;

        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        rootView = LayoutInflater.from(getApplication()).inflate(R.layout.camera_picture_view_layout,null);
        windowManager.addView(rootView,layoutParams);

        cameraView = (CameraView) rootView.findViewById(R.id.camera_view);
    }


    /**
     *  提交照片名字和路径到服务器
     * @param name
     * @param path
     */
    private void commitPicPath(final String name, final String path,String projectid){
        LogUtil.v("name:"+name+"  path:"+path+"  projectId:"+projectid);
        DataInterface dataInterface = new DataImpl();
        dataInterface.commitPicPath(Constants.PICTURE_URL, name, path,projectid, new StringCallback() {
            @Override
            public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                LogUtil.v("s:"+s);
                stopSelf();
            }

            @Override
            public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                super.onError(isFromCache, call, response, e);
                 LogUtil.v("error:"+response);
                stopSelf();
            }
        }, this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
